package com.zidian.teacher.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.Menu;
import android.widget.PopupMenu;
import android.widget.RadioButton;
import android.widget.RadioGroup;


import com.zidian.teacher.R;
import com.zidian.teacher.fragment.CourseFragment;

import java.util.Timer;
import java.util.TimerTask;

import com.zidian.teacher.fragment.PersonFragment;
import com.zidian.teacher.fragment.QuestionnaireFragment;
import com.zidian.teacher.fragment.TeachingEvaluationFragment;
import com.zidian.teacher.fragment.TeachingFragment;
import com.zidian.teacher.common.ActManager;
import com.zidian.teacher.common.AppActivity;
import com.zidian.teacher.common.BaseFragment;


public class MainActivity extends AppActivity {


    /* ============Model============== */
    private PopupMenu popupMenu;
    private Menu menu;

    /**
     * 判断跳转到哪个页面的下标
     */
    private int switchid = 0;
    /**
     * Fragment管理器
     */
    private FragmentManager fragmentManager;
    /**
     * 界面下方四个单选按钮组
     */
    private RadioGroup radio_group;
    /* ============Control================= */
    /**
     * 四个按钮的单击监听
     */
    private RadioGroup.OnCheckedChangeListener mOnCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup arg0, int keyCode) {
            /* 处理之前的文字 */
            ((RadioButton) arg0.getChildAt(switchid)).setTextColor(0xff7f7f7c);
            /* 处理被单击的按钮 */
            RadioButton mRadioButton = (RadioButton) arg0.findViewById(keyCode);
            mRadioButton.setTextColor(Color.parseColor("#f74a4e"));

            switchid = Integer.parseInt(mRadioButton.getTag().toString());
            /* 设置跳转那个fragment页面 */
            switch (switchid) {
                case 0:
                    setTitle("课程");
                    addFragment(CourseFragment.newInstance());
                    break;
                case 1:
                    setTitle("教评");
                    addFragment(TeachingEvaluationFragment.newInstance());
                    break;
                case 2:
                    setTitle("问卷");
                    addFragment(QuestionnaireFragment.newInstance());
                    break;
                case 3:
                    setTitle("我");
                    addFragment(PersonFragment.newInstance());
                    break;
            }

        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        radio_group = (RadioGroup) findViewById(R.id.radio_group);
        radio_group.setOnCheckedChangeListener(mOnCheckedChangeListener);
        ((RadioButton) radio_group.getChildAt(0)).setChecked(true);

    }


    @Override
    protected BaseFragment getFirstFragment() {
        return CourseFragment.newInstance();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        super.onKeyDown(keyCode, event);
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
        }
        return false;
    }

    // 双击退出程序
    private boolean isExit = false;

    private void exit() {
        if (!isExit) {
            isExit = true;
            Snackbar.make(radio_group,"再按一次退出程序",Snackbar.LENGTH_SHORT).show();
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false;
                }
            }, 2000);
        } else {
            // 退出
            ActManager.getAppManager().AppExit(context);
        }
    }


}
